package com.atguigu.activemq.topic_persist;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JmsConsumer_Topic_persist {
    public static final String ACTIVEMQ_URL="tcp://124.70.84.192:61616";
    public static final String TOPIC_NAME="topic_lemon";
    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        //连接先不启动；
        connection.setClientID("z3");//设置订阅者id
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "备注");
        //设置持久化的订阅者，第一个参数填目的地(队列或者主题)，第二个参数填备注，备注不重要
        connection.start();//此时连接启动
        System.out.println("1号消费者z3");
        Message message = topicSubscriber.receive();//等待消息(不写参数一直等)
        while (null!=message){
            TextMessage textMessage= (TextMessage) message;
            System.out.println("订阅的消息:"+textMessage.getText());
            message=topicSubscriber.receive();
            //消费完消息之后继续等待消息，达到类似于监听的作用；
        }
        session.close();
        connection.close();
    }
}
